package com.shushan.invitecode.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.shushan.invitecode.bean.InviteBean;
import com.shushan.invitecode.bean.PatchBean;
import com.shushan.shushanbao.R;

import java.util.List;

/**
 * @ClassName: RetroCardAdapter
 * @Desciption: //补签卡适配器
 * @author: zhangshihao
 * @date: 2018-09-15
 */
public class RetroCardAdapter extends RecyclerView.Adapter<RetroCardAdapter.ViewHolder>{

    private static final String TAG = RetroCardAdapter.class.getSimpleName();

    private List<PatchBean> patchBeans;

    private OnUseBtnClickListener onUseBtnClickListener;

    public RetroCardAdapter(InviteBean inviteBean){
        this.patchBeans = inviteBean.getPatchList();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewHolder holder = new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_retro_cards,null,false));
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        PatchBean patchBean = patchBeans.get(position);
        holder.tvDate.setText(patchBean.create_time);
        holder.tvCardOrigin.setText(getCardOriginStr(patchBean));
        if(patchBean.is_use == 1){
            holder.btnUse.setBackgroundResource(R.drawable.bg_btn_used);
            holder.btnUse.setText("已使用");
        }else{
            holder.btnUse.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(onUseBtnClickListener != null){
                        onUseBtnClickListener.onUseBtnClick();
                    }
                }
            });
        }
    }

    private String getCardOriginStr(PatchBean patchBean){
        StringBuffer sb = new StringBuffer();
        if(patchBean.user == 0){
            sb.append("好友");
            String phone = patchBean.phone;
            String phone1 = phone.substring(0,3);
            String phone2 = phone.substring(7,11);
            sb.append(phone1+"****"+phone2);
            sb.append("充值成为包年会员，你获得一张补签卡");
        }else{
            sb.append("我付费")
              .append((int)patchBean.money+"元，获得一张补签卡");
        }
        return sb.toString();
    }

    @Override
    public int getItemCount() {
        return patchBeans.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        private TextView tvDate;
        private TextView tvCardOrigin;
        private Button btnUse;
        public ViewHolder(View itemView) {
            super(itemView);
            tvDate = itemView.findViewById(R.id.tv_date);
            tvCardOrigin = itemView.findViewById(R.id.tv_card_origin);
            btnUse = itemView.findViewById(R.id.btn_use);
        }
    }

    public void setOnUseBtnClickListener(OnUseBtnClickListener onUseBtnClickListener) {
        this.onUseBtnClickListener = onUseBtnClickListener;
    }

    public interface OnUseBtnClickListener{
        void onUseBtnClick();
    }

}
